package com.avamanager.app.service.impl;

import com.avamanager.app.domain.AppStudent;
import com.avamanager.app.domain.AppStudentUser;
import com.avamanager.app.mapper.AppStudentMapper;
import com.avamanager.app.mapper.AppStudentUserMapper;
import com.avamanager.app.service.IAppStudentService;
import com.avamanager.common.core.domain.entity.SysUser;
import com.avamanager.common.utils.DateUtils;
import com.avamanager.common.utils.StringUtils;
import com.avamanager.system.domain.SysUserPost;
import com.avamanager.system.domain.SysUserRole;
import com.avamanager.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.avamanager.system.mapper.SysUserMapper;
import com.avamanager.system.service.ISysUserService;

import java.util.ArrayList;
import java.util.List;

/**
 * 学生信息Service业务层处理
 * 
 * @author FenvyYe
 * @date 2022-08-14
 */
@Service
public class AppStudentServiceImpl implements IAppStudentService 
{
    @Autowired
    private AppStudentMapper appStudentMapper;

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private AppStudentUserMapper appStudentUserMapper;

    /**
     * 查询学生信息
     * 
     * @param studentId 学生信息主键
     * @return 学生信息
     */
    @Override
    public AppStudent selectAppStudentByStudentId(Long studentId)
    {
        return appStudentMapper.selectAppStudentByStudentId(studentId);
    }

    /**
     * 查询学生信息列表
     * 
     * @param appStudent 学生信息
     * @return 学生信息
     */
    @Override
    public List<AppStudent> selectAppStudentList(AppStudent appStudent)
    {
        return appStudentMapper.selectAppStudentList(appStudent);
    }

    /**
     * 新增学生信息
     * 
     * @param appStudent 学生信息
     * @return 结果
     */
    @Override
    public int insertAppStudent(AppStudent appStudent)
    {
        appStudent.setCreateTime(DateUtils.getNowDate());
        insertStudentUser(appStudent);//插入关联表
        return appStudentMapper.insertAppStudent(appStudent);
    }

    /**
     * 修改学生信息
     * 
     * @param appStudent 学生信息
     * @return 结果
     */
    @Override
    public int updateAppStudent(AppStudent appStudent)
    {
        appStudent.setUpdateTime(DateUtils.getNowDate());
        Long studentId = appStudent.getUserId();
        // 删除学生与用户关联
        appStudentUserMapper.deleteStudentUserByStudentId(studentId);
        // 新增学生与用户关联
        insertStudentUser(appStudent);
        return appStudentMapper.updateAppStudent(appStudent);
    }

    /**
     * 批量删除学生信息
     * 
     * @param studentIds 需要删除的学生信息主键
     * @return 结果
     */
    @Override
    public int deleteAppStudentByStudentIds(Long[] studentIds)
    {
        // 删除学生与用户关联
        appStudentUserMapper.deleteStudentUser(studentIds);
        return appStudentMapper.deleteAppStudentByStudentIds(studentIds);
    }

    /**
     * 删除学生信息信息
     * 
     * @param studentId 学生信息主键
     * @return 结果
     */
    @Override
    public int deleteAppStudentByStudentId(Long studentId)
    {
        // 删除学生与用户关联
        appStudentUserMapper.deleteStudentUserByStudentId(studentId);
        return appStudentMapper.deleteAppStudentByStudentId(studentId);
    }

    /**
     * 新增用户角色信息
     *
     * @param studentId 学生ID
     * @param userIds 用户组
     */
    public void insertStudentUser(Long studentId, Long[] userIds)
    {
        if (StringUtils.isNotEmpty(userIds))
        {
            // 新增学生与用户关联
            List<AppStudentUser> list = new ArrayList<AppStudentUser>(userIds.length);
            for (Long userId : userIds)
            {
                AppStudentUser ur = new AppStudentUser();
                ur.setStudentId(studentId);
                ur.setUserId(userId);
                list.add(ur);
            }
            appStudentUserMapper.batchStudentUser(list);
        }
    }

    public void insertStudentUser(AppStudent student)
    {
        this.insertStudentUser(student.getStudentId(), student.getUserIds());
    }




}
